#include "link.h"
/*加入注释*/


void LinkedList::insert(int i, eleType value){
    if(i<0||i>size){
        throw out_of_range("Invalid position");
    }
    Linknode newNode = new Node(value);
    if (i==0){//插入头节点情况
        newNode->next = head;
        head = newNode;
    }else{//插入不是头结点
        Linknode curr = head;
        for (int j=0;j<i-1;j++){
            curr = curr->next;
        }
        newNode->next = curr->next;
        curr->next = newNode;
    }
    ++size;
}



void LinkedList::remove(int i){
    if(i<0||i>=size){
        throw out_of_range("Invalid position");
    }
    if (i == 0){
        Linknode tmp = head;
        head = head->next;
        delete tmp;
    }else{
        Linknode curr = head;
        for (int j=0; j<i-1;j++){
            curr = curr->next;
        }
        Linknode tmp = curr->next;
        curr->next = tmp->next;
        delete tmp;
    }
    size--;
}

Linknode LinkedList::find(eleType value){
    Linknode curr = head;
    while (curr && curr->data != value)
    {
        curr = curr->next;
    }
    return curr;
    
}

Linknode LinkedList::get(int i){
    if(i<0||i>=size){
        throw out_of_range("Invalid position");
    }
    Linknode curr = head;
    for (int j=0; j<i;j++){
        curr = curr->next;
    }
    return curr;
}

void LinkedList::update(int i, eleType value){
    get(i)->data = value;
}

void LinkedList::print(){
    Linknode curr = head;
    while (curr)
    {
        cout<<curr->data<<" "<<curr->next<<" ";

        curr = curr->next;
    }
    cout<<endl;
}